/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jhotdraw.samples.phototemplate;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.SwingWorker;
import org.imgscalr.Scalr;

/**
 *
 * @author Eliazar
 */
public class LoadFilesWorker extends SwingWorker<Integer, String> {
    private List files;    
    private JPanel panelMiniPhoto;
    private JScrollPane scrollPane;
    private final int sizeThumbnail = 70;
    private Map mapThumbails;
    private JPanel panel;
    private JLabel feedback;
    private GridBagConstraints gbcPanelTumbnails;
    
    public LoadFilesWorker(List files, Map mapThumbnails, JPanel panelThumbnails, JScrollPane scrollPane, JPanel panelProgess) {
        this.files = files;        
        panelMiniPhoto = panelThumbnails;           
        this.mapThumbails = mapThumbnails;
        this.scrollPane = scrollPane;
        this.panel = panelProgess;
        gbcPanelTumbnails = getGridBagConstraints();
        for (Component component : panel.getComponents()) {
            if(component instanceof JLabel) {
                feedback = (JLabel)component;
            }
        }
    }        
    
    @Override
    protected Integer doInBackground() throws Exception {
        if(files != null && !files.isEmpty()) {
            
            int count = 0;
            File file = null;
            for (Object object : files) {
                try {
                    file = (File) object;
                    count++;
                    publish("Carregando o arquivo " +  file.getName() + ".");
                    setProgress((100*count) / files.size());
                    BufferedImage bi = ImageIO.read((File)file);
                    createThumbail(file, bi);                           
                    publish("Arquivo " + file.getName() + " carregado com sucesso.");
                }catch(IOException e) {
                    if(file != null) {
                        publish("Arquivo " + file.getName() + " não foi carregado.");
                    }
                }
            }                        
        }
        return 1;
    }       

    @Override
    protected void process(List<String> chunks) {        
        for (String string : chunks) {
            feedback.setText(string);
        }        
    }

    
    private void createThumbail(File file, BufferedImage bi) {                
        JLabel label;                        
        label = new JLabel(new ImageIcon(Scalr.resize(bi, sizeThumbnail, sizeThumbnail)));
        label.setMinimumSize(new Dimension(sizeThumbnail, sizeThumbnail));
        mapThumbails.put(file.toURI(), label);                 
        panelMiniPhoto.add(label, gbcPanelTumbnails);        
        gbcPanelTumbnails.gridy = panelMiniPhoto.getComponents().length;
        
        JViewport viewport = new JViewport();
        viewport.setPreferredSize(new Dimension(100, panelMiniPhoto.getComponents().length * sizeThumbnail));
        viewport.add(panelMiniPhoto);
        scrollPane.setViewportView(viewport);
        panelMiniPhoto.invalidate();
        panelMiniPhoto.validate();
                
    }
    
    private GridBagConstraints getGridBagConstraints() {
        GridBagConstraints gbcPanelTumbnails = new GridBagConstraints();
        gbcPanelTumbnails.insets = new Insets(2, 2, 2, 2);
        gbcPanelTumbnails.gridx = 0;
        if(panelMiniPhoto != null && panelMiniPhoto.getComponents() != null) {
            gbcPanelTumbnails.gridy = panelMiniPhoto.getComponents().length;
        }else {
            gbcPanelTumbnails.gridy = 0;
        }
        return gbcPanelTumbnails;
    }
}
